/**
 * Created by CJB on 2015/11/12.
 */
/*$(function() {*/
var dojoConfig = {
    async: true
};

/**
 * 项目基础路径
 * @type {string}
 */
var basePath = getPath();
/**
 * 地图
 */
var map;
/**
 * 画图工具
 */
var drawTool;
/**
 * 地图服务地址
 * @type {string}
 */
var webGis = "";
/**
 * 图片图表基础大小
 * @type {number}
 */
var pictureNormalSize = 20;

/**
 * 初始化地图配置<br>
 * 使用之前需要加载如下js,图层部分按需加载
 * <p>
 * 	dojo.require("esri.map");<br>
 * 	dojo.require("kingtop.layers.cacheMap");天地图<br>
 * 	dojo.require("kingtop.layers.ArcgisBundleLayer");天地图<br>
 * 	dojo.require("esri.layers.ArcGISDynamicMapServiceLayer");<br>
 * 	dojo.require("kingtop.layers.KingMapWMSLayer");<br>
 * 	dojo.require("esri.layers.FeatureLayer");<br>
 * 	dojo.require("esri.layers.wms");<br>
 * 	dojo.require("kingtop.pop.InfoWindow");地图上的弹出显示<br>
 * 	dojo.require("dojo.dom");<br>
 * 	dojo.require("dojo.dom-construct");<br>
 * 	dojo.require("dojo.string");<br>
 * 	dojo.require("dojo.domReady!");<br>
 * </p>
 * @param loadListener 监听图层是否加载完成
 */
function initMap(loadListener, layerAddResultListener) {
    map = new esri.Map("map", {
        logo: false,
        slider: false
    });
    /*esri.urlUtils.addProxyRule({
     urlPrefix: "http://10.161.1.48:8080/OneMapServer",
     proxyUrl: "http://10.160.131.2:9080/Java/proxy.jsp"
     });*/
    $.getJSON(getPath() + "config/mapConfig.json", function (data) {
        //分别设置地图放大缩小的持续时间和刷新时间设置
        // esriConfig.defaults.map.panDuration = 500;//默认350
        // esriConfig.defaults.map.zoomDuration = 700;//默认500
        //分别设置地图移动的持续时间和刷新时间
        // esriConfig.defaults.map.panRate = 50;//默认25
        // esriConfig.defaults.map.zoomRate = 50;//默认25

        var infoWindow = new kingtop.pop.InfoWindow({
            domNode: dojo.create("div", null, dojo.dom.byId("map"))
        });
        map.infoWindow = infoWindow;
        map.on("load", function () {
            if (typeof (eval(loadListener)) == "function")
                loadListener();
            /*var overviewMapDijit = new esri.dijit.OverviewMap({
                map: map,
                attachTo: "bottom-right",
                visible: true
            });
            overviewMapDijit.startup();*/
        });
        map.on("layer-add-result",function(event){
            if (typeof (eval(layerAddResultListener)) == "function")
                layerAddResultListener(event);
        });

        webGis = data.webGis;
        if(data.map.initialextent){
            var initialextent = data.map.initialextent;//初始化范围
            initialextent = initialextent.split(",");
            var extent = new esri.geometry.Extent({
                xmin: parseFloat(initialextent[0]),
                ymin: parseFloat(initialextent[1]),
                xmax: parseFloat(initialextent[2]),
                ymax: parseFloat(initialextent[3]),
                spatialReference:{wkid:data.map.wkid}
            });
            map.setExtent(extent);
        }

        var basemaps = data.map.basemaps;
        var layers = new Array(basemaps.length);
        for (var i = 0; i < basemaps.length; i++) {
            var value = basemaps[i];
            var mapLayer;
            switch (value.type) {
                case "tianditu":
                    mapLayer = new kingtop.layers.cacheMap(value.baseurl, value.label, value.fileType);
                    mapLayer.id = value.id;
                    mapLayer.visible = value.visible;
                    layers[i] = mapLayer;
                    break;
                case "dynamic":
                    mapLayer = new esri.layers.ArcGISDynamicMapServiceLayer(value.baseurl,{
                        id: value.id
                    });
                    if(value.alpha)
                        mapLayer.opacity = value.alpha;
                    if(value.visible != undefined)
                        mapLayer.visible = value.visible;
                    layers[i] = mapLayer;
                    break;
                case "feature":
                    mapLayer = new esri.layers.FeatureLayer(value.baseurl,{
                        id: value.id,
                        outFields: ["*"],
                        mode: esri.layers.FeatureLayer.MODE_SNAPSHOT
                    });
                    if(value.alpha)
                        mapLayer.opacity = value.alpha;
                    if(value.visible != undefined)
                        mapLayer.visible = value.visible;
                    layers[i] = mapLayer;
                    break;
                case  "arcgisBundle":
                    mapLayer = new kingtop.layers.ArcgisBundleLayer(getPath() + value.baseurl);
                    mapLayer.id = value.id;
                    mapLayer.visible = value.visible;
                    if(value.alpha)
                        mapLayer.opacity = value.alpha;
                    layers[i] = mapLayer;
                    break;
                case  "wms":
                    var extentArr = value.extent.split(",");
                    /*var resourceInfo = {
                        extend: new esri.geometry.Extent(parseFloat(extentArr[0]),parseFloat(extentArr[1]),parseFloat(extentArr[2]),parseFloat(extentArr[3]),{
                            wkid: parseInt(value.wkid)
                        })
                    };*/
                    //mapLayer = new esri.layers.WMSLayer(getPath() + value.baseurl);
                    /*mapLayer = new esri.layers.WMSLayer(getPath() + value.baseurl,{
                        resourceInfo:resourceInfo,
                        visibleLayers: value.layers.split(",")
                    });*/
                    mapLayer = new kingtop.layers.KingMapWMSLayer(getPath() + value.baseurl,{
                        layers:value.layers,
                        wkid:value.wkid,
                        remoteUrl:value.remoteUrl,
                        styles: value.styles,
                        fullExtent: {
                        	xmin: parseFloat(extentArr[0]),
                        	ymin: parseFloat(extentArr[1]),
                        	xmax: parseFloat(extentArr[2]),
                        	ymax: parseFloat(extentArr[3])
                        },
                        initialExtent: {
                        	xmin: parseFloat(extentArr[0]),
                        	ymin: parseFloat(extentArr[1]),
                        	xmax: parseFloat(extentArr[2]),
                        	ymax: parseFloat(extentArr[3])
                        }
                    });
                    mapLayer.id = value.id;
                    mapLayer.visible = value.visible;
                    if(value.alpha)
                        mapLayer.opacity = value.alpha;
                    layers[i] = mapLayer;
                    break;
                case "tiledMap":
                    mapLayer = new esri.layers.ArcGISTiledMapServiceLayer(value.baseurl,{
                        id: value.id
                    });
                    if(value.alpha)
                        mapLayer.opacity = value.alpha;
                    if(value.visible != undefined)
                        mapLayer.visible = value.visible;
                    layers[i] = mapLayer;
                    break;
                case "tdtwms":
                	mapLayer = new kingtop.layers.TiandituLayer(value.baseurl);
                	mapLayer.id = value.id;
                	if(value.alpha)
                		mapLayer.opacity = value.alpha;
                	if(value.visible != undefined)
                		mapLayer.visible = value.visible;
                	layers[i] = mapLayer;
                	break;
            }
        }
        map.addLayers(layers);
        initMapTools(basemaps,"mapTools");
    });
    return map;
}

/**
 * 初始化地图工具栏
 * @param basemaps 地图图层
 * @param divId div的id
 */
function initMapTools(basemaps, divId){
    if(divId && basemaps){
        var targetObject = $("#" + divId);
        //var offset = targetObject.parent().offset();
        //targetObject.css("top", offset.top + 15);
        targetObject.css("top", 15);
        //初始化内容
        var toolsHtml = "<div class='tools map-layer'><span class='icon layer'></span><i>图层</i><em ></em></div>";
        //toolsHtml += "<b></b>";
        toolsHtml += "<div class='tools box'><span class='icon boxutils'></span><i>工具</i><em ></em></div>";
        var detailHtml = "<div class='map-layer-detail detail' style='display: none;padding: 5px 12px 10px 5px'><ul></ul></div>";
        detailHtml += "<div class='detail box-detail' style='display: none'><ul>";
        detailHtml += "<li><span class='icon calc'></span><i>测距</i></li>";
        detailHtml += "<li><span class='icon calc-area'></span><i>测面</i></li>";
        detailHtml += "<li><span class='icon clear'></span><i>清空</i></li>";
        detailHtml += "</ul></div>";
        $("#" + divId).append(toolsHtml);
        $("#" + divId).append(detailHtml);
        //初始化地图图层和事件
        var layerDiv = $(".map-tools .map-layer-detail ul");
        layerDiv.css({"padding": "0", "margin": "0"});
        $(".box-detail ul").css({"padding": "0", "margin": "0"});
        for (var i = 0; i < basemaps.length; i++) {
            var value = basemaps[i];
            addMapToolLayer(value);
        }
        //编辑点击菜单事件
        $("div.map-layer").click(function(event){
            $(".map-tools>div").removeClass("selected");
            if($(".map-layer-detail").css("display") == "none"){
                $(this).addClass("selected");
                $(".map-tools .detail").css("display","none");
                $(".map-layer-detail").css("display","block");
            }else{
                $(".map-layer-detail").css("display","none");
            }
            event.stopPropagation();
        });
        $("div.box").click(function(event){
            $(".map-tools>div").removeClass("selected");
            if($(".box-detail").css("display") == "none"){
                $(this).addClass("selected");
                $(".map-tools .detail").css("display","none");
                $(".box-detail").css("display","block");
            }else{
                $(".box-detail").css("display","none");
            }

            event.stopPropagation();
        });
        $(".calc").parent().click(function(event){
            measureLine();
        });
        $(".calc-area").parent().click(function(event){
            measureArea();
            //event.stopPropagation();
        });
        $(".clear").parent().click(function(){
            clearMeasureLineLayer();
        });
        //清空弹出框
        $("body").bind("mousedown",{}, onBodyMouseDown);
    }
}

/**
 * 隐藏树形菜单
 * @param event
 */
function onBodyMouseDown(event) {
    if (!($(event.target).hasClass("detail") || $(event.target).hasClass("tools") || $(event.target).parents(".detail").length>0 || $(event.target).parents(".tools").length>0)) {
        $(".map-tools>div").removeClass("selected");
        $(".map-tools .detail").css("display","none");
    }
}

/**
 * 添加到地图工具栏的图层控制
 * @param value 图层信息
 */
function addMapToolLayer(value){
    var layerHtml = "<li><div><input id='check_" + value.id + "' type='checkbox'"+(value.visible ? "checked='checked'" : "")+"><i>" +
        value.name + "</i></div><div id='slider_" + value.id + "' class='slider'></div>";
    $(".map-tools .map-layer-detail ul").append(layerHtml);
    $("#check_" + value.id).change(function(){
        var id = $(this).attr("id");
        var layerId = id.toString().substring(6);
        var currentLayer = getLayer(layerId);
        if(currentLayer)
            currentLayer.setVisibility(this.checked);
    });
    //滑动条滑动，改变图层透明度
    $("#slider_" + value.id).slider({
        range: "min",
        min: 0,
        max: 100,
        value: value.alpha ? value.alpha * 100 : 100,
        slide: function(event, ui){
            var id = $(ui.handle).parent().attr("id");
            var layerId = id.toString().substring(7);
            var currentLayer = getLayer(layerId);
            if(currentLayer) {
                currentLayer.setOpacity(ui.value / 100);
            }
        }
    });
}

var drawEnd = null;

/**
 * 初始化地图画笔使用之前需要加载如下js
 * <p>
 * 	dojo.require("esri.toolbars.draw");
 * </p>
 * @param drawEndListener 画图完成监听,参数evtObj包含geometry
 */
function initDraw(drawEndListener) {
    if (map != null && typeof (map) != undefined) {
    	if(drawTool == null){
	        drawTool = new esri.toolbars.Draw(map);
	        drawTool.on("draw-end", function (evtObj) {
	            drawTool.deactivate();
	            if (typeof (eval(drawEndListener)) == "function")
	                drawEndListener(evtObj);
	            if(drawEnd){
	                drawEnd(evtObj);
	                drawEnd = null;
	            }
	        });
    	}
    }
}

/**
 * 启用绘图功能，绘制之前，调用initDraw初始化地图画笔
 * 使用之前需要加载如下js
 * <p>
 * 	dojo.require("esri.toolbars.draw");
 * </p>
 * @param point | multipoint | polyline | polygon | freehandpolyline | freehandpolygon | line | extent | arrow | circle | downarrow | ellipse | leftarrow | rectangle | rightarrow | triangle | uparrow
 */
function activeDraw(type, drawEndListener) {
    if(drawTool != null){
        var allType = "point | multipoint | polyline | polygon | freehandpolyline | freehandpolygon | line | extent | arrow | " +
            "circle | downarrow | ellipse | leftarrow | rectangle | rightarrow | triangle | uparrow";
        drawEnd = drawEndListener;
        if (allType.indexOf(type) > -1)
            drawTool.activate(type);
    }
}

/**
 * 设置图层显示与否
 * @param layerId 图层id
 * @param options 包含参数：visible 是否显示, method:比较方法，分别为equal,like
 */
function setLayerVisible(layerId, options) {
    options = $.extend({
        visible: true,
        method: "equal"
    }, options || {});
    if (map != null && typeof (map) != undefined) {
        if (options.method == "equal") {
            var layer = map.getLayer(layerId);
            if (layer != null && typeof (layerId) != undefined) {
                layer.setVisibility(options.visible);
            }
        } else if (options.method == "like") {
            var layserIds = map.layerIds;
            $.each(layserIds, function (index, value) {
                if (value.indexOf(layerId) > -1) {
                    var layer = map.getLayer(value);
                    layer.setVisibility(options.visible);
                }
            });
        }
    }
}

/**
 * 根据图层id获取指定图层
 * @param layerId 图层id
 * @returns {null}
 */
function getLayer(layerId) {
    if (map != null && typeof (map) != undefined) {
        return map.getLayer(layerId);
    } else
        return null;
}

/**
 * 添加图层，GraphicsLayer
 * 使用之前需要加载如下js
 * <p>
 * 	dojo.require("esri.layers.GraphicsLayer");
 * </p>
 * @param layerId 图层id
 */
function addLayer(layerId, options) {
    var layer = null;
    options = $.extend({
        mouseOverListener: null,
        mouseOutListener: null,
        mouseClickListener: null,
		mouseMoveListener: null,
        infoTemplate: null,
        showOnTools: false,
        info:{
            name:"",
            visible: true,
            alpha: 100
        }
    }, options || {});
    if (map != null && typeof (map) != undefined) {
        layer = new esri.layers.GraphicsLayer({id: layerId});
        map.addLayer(layer);
        if (options.mouseOverListener != null && typeof (eval(options.mouseOverListener)) == "function")
            layer.on("mouse-over", options.mouseOverListener);
        if (options.mouseOutListener != null && typeof (eval(options.mouseOutListener)) == "function")
            layer.on("mouse-out", options.mouseOutListener);
        if (options.mouseClickListener != null && typeof (eval(options.mouseClickListener)) == "function")
            layer.on("click", options.mouseClickListener);
		if (options.mouseMoveListener != null && typeof (eval(options.mouseMoveListener)) == "function")
			layer.on("mouse-move", options.mouseMoveListener);
        if(options.showOnTools){
            addMapToolLayer({
                id:layerId,
                name:options.info.name,
                visible: options.info.visible,
                alpha: options.info.alpha
            });
        }
    }
    return layer;
}
/**
 * 清空图层
 * @param layerId 图层id
 * @param options {infoWindow: 是否清空弹出窗}
 */
function clearLayer(layerId, options) {
	options = $.extend({
		infoWindow: true
	}, options||{});
	var layer =  getLayer(layerId);
	if(layer)
		layer.clear();
	if(options.infoWindow)
		showInfoWindow();
}
/**
 * 使用之前需要加载如下js
 * <p>
 *     dojo.require("esri.geometry.Point");
 *     dojo.require("esri.SpatialReference");
 * </p>
 * @param lon 经度
 * @param lat 纬度
 * @param wkid
 */
function createPointGeometry(lon, lat, wkid){
    wkid = wkid || map.spatialReference.wkid;
    return new esri.geometry.Point(lon, lat, new esri.SpatialReference(wkid));
}

/**
 * 添加点
 * 使用之前需要加载如下js
 * <p>
 * 	dojo.require("esri.graphic");
 *  dojo.require("esri.geometry.Point");
 * </p>
 * @param layerId 图层id
 * @param lon 经度
 * @param lat 纬度
 * @param options 其他参数{symbol:图标,attributes:属性}
 */
var addPoint = function (layerId, lon, lat, options) {
    var layer = getLayer(layerId);
    var graphic = null;
    if (layer != null) {
        options = $.extend({
            symbol: getSimpleMarkerSymbol(),
            attributes: {}
        }, options || {});
        var graphic = new esri.graphic(new esri.geometry.Point(lon, lat, map.spatialReference), options.symbol, options.attributes);
        layer.add(graphic);
    }
    return graphic;
};

/**
 * 添加多边形
 * 使用之前需要加载如下js
 * <p>
 * 	dojo.require("esri.graphic");
 * 	dojo.require("esri.geometry.Polygon");
 *  dojo.require("esri.symbols.SimpleFillSymbol");
 *  dojo.require("esri.symbols.SimpleLineSymbol");
 * 	dojo.require("esri.Color");
 * </p>
 * @param layerId 图层id
 * @param rings 数组的数组的数据，三层结构，示例数据如下[[[118,24],[119,24]],[[117,23],[119,23]],[[113,24],[114,24]]]
 * @param options
 * @returns {*}
 */
function addPolygon(layerId, rings, options){
    var layer = getLayer(layerId);
    var graphic = null;
    if (layer != null && rings && rings.length) {
        options = $.extend({
            symbol: getSimpleFileSymbol(),
            attributes: {}
        }, options || {});

        var polygon = new esri.geometry.Polygon({
            "rings":rings,
            "spatialReference": {
                "wkid": map.spatialReference.wkid
            }
        });
        graphic = new esri.graphic(polygon, options.symbol, options.attributes);
        layer.add(graphic);
    }
    return graphic;
}
/**
 * 添加线
 * 使用之前需要加载如下js
 * 	dojo.require("esri.graphic");
 * 	dojo.require("esri.geometry.Polyline");
 *  dojo.require("esri.symbols.SimpleLineSymbol");
 * 	dojo.require("esri.Color");
 * @param layerId 图层id
 * @param paths 数组的数组的数据，三层结构，示例数据如下[[[118,24],[119,24]],[[117,23],[119,23]],[[113,24],[114,24]]]
 * @param options
 * @returns
 */
function addPolyline(layerId, paths, options) {
	var layer = getLayer(layerId);
	var graphic = null;
	if(layer != null && paths && paths.length) {
		options = $.extend({
			symbol: getSimpleLineSymbol(),
			attributes: {}
		}, options || {});
		var polyline = new esri.geometry.Polyline({
			"paths": paths,
			"spatialReference": {
				"wkid": map.spatialReference.wkid
			}
		});
		graphic = new esri.graphic(polyline, options.symbol, options.attributes);
		layer.add(graphic);
	}
	return graphic;
}
/**
 * 添加图标
 * 使用之前需要加载如下js
 * <p>
 * 	dojo.require("esri.graphic");
 * 	dojo.require("esri.symbols.PictureMarkerSymbol");
 * 	dojo.require("esri.symbols.SimpleLineSymbol");
 * 	dojo.require("esri.symbols.SimpleFillSymbol");
 * </p>
 * @param layerId 图层id
 * @param geometry 要素,点线面等
 * @param options 可选参数，symbol:图标， attributes：属性
 */
function addGraphic(layerId, geometry, options) {
    var layer = getLayer(layerId);
    var graphic = null;
    if (layer != null) {
        options = $.extend({
            symbol: null,
            attributes: {}
        }, options || {});
        if (options.symbol == null) {
            switch (geometry.type) {
                case "point":
                case "multipoint":

                    options.symbol = getPictuteMarkerSymbol();
                    break;
                case "polyline":
                case "freehandpolyline":
                case "line":
                    options.symbol = getSimpleLineSymbol();
                    break;
                case "polygon":
                case "freehandpolygon":
                case "extent":
                case "arrow":
                case "circle":
                case "downarrow":
                case "ellipse":
                case "leftarrow":
                case "rectangle":
                case "rightarrow":
                case "triangle":
                case "uparrow":
                    options.symbol = getSimpleFileSymbol();
                    break;
            }
        }
        graphic = new esri.graphic(geometry, options.symbol, options.attributes);
        layer.add(graphic);
    }
    return graphic;
}

/**
 * g根据坐标进行居中
 * 使用之前需要加载如下js
 * <p>
 * 	dojo.require("esri.geometry.Point");
 * </p>
 * @param lon
 * @param lat
 */
function centerPoint(lon, lat, level) {
    var point = new esri.geometry.Point(lon, lat, map.spatialReference);
    if(!level)
        map.centerAt(point);
    else
        map.centerAndZoom(point, level);
}

/**
 * 居中图层中所有元素
 * 使用之前需要加载如下js
 * <p>
 * 	dojo.require("esri.graphicsUtils");
 * </p>
 * @param lyaerId 图层id
 */
function centerLayer(layerId) {
    var layer = getLayer(layerId);
    if (layer != null) {
        var extent = esri.graphicsUtils.graphicsExtent(layer.graphics);
        map.setExtent(extent, true);
    }
}

/**
 * 获取线符号，默认黑色实线，宽度1
 * 使用之前需要加载如下js
 * <p>
 * 	dojo.require("esri.symbols.SimpleLineSymbol");
 * 	dojo.require("esri.Color");
 * </p>
 * @param options 参数，style可选值有dash | dashdot | longdashdotdot | dot | longdash | longdashdot | none | shortdash | shortdashdot | shortdashdotdot | shortdot | solid，
 * color边框颜色，width宽度
 */
function getSimpleLineSymbol(options) {
    options = $.extend({
        style: esri.symbols.SimpleLineSymbol.STYLE_SOLID,
        color: "#000000",
        width: 1
    }, options || {});
    return new esri.symbols.SimpleLineSymbol(options.style, new esri.Color(options.color), options.width);
}

/**
 * 获取简单点符号，默认圆形，红色填充，黑色边框
 * 使用之前需要加载如下js
 * <p>
 * 	dojo.require("esri.symbols.SimpleMarkerSymbol");
 * 	dojo.require("esri.Color");
 * </p>
 * @param options 参数，style可选值有 circle | cross | diamond | path | square | x，
 * size大小，outline外边框，color填充色
 */
function getSimpleMarkerSymbol(options) {
    options = $.extend({
        style: esri.symbols.SimpleMarkerSymbol.STYLE_CIRCLE,
        size: 10,
        outline: getSimpleLineSymbol(),
        color: "#ff0000"
    }, options || {});
    return new esri.symbols.SimpleMarkerSymbol(options.style, options.size, options.outline, new esri.Color(options.color));
}

/**
 * 获取图片图标，默认图钉图标，宽高都为20
 * 使用之前需要加载如下js
 * <p>
 * 	dojo.require("esri.symbols.PictureMarkerSymbol");
 * </p>
 * @param options 可选参数，url：图片地址，width：宽度， height：高度
 */
function getPictuteMarkerSymbol(options) {
    options = $.extend({
        url: getPath() + "statics/images/gis/default_symbol_img.png",
        width: pictureNormalSize,
        height: pictureNormalSize,
        yoffset:0,
        xoffset:0
    }, options || {});
    var pictuteMarkerSymbol = new esri.symbols.PictureMarkerSymbol(options.url, options.width, options.height);
    pictuteMarkerSymbol.yoffset = options.yoffset;
    pictuteMarkerSymbol.xoffset = options.xoffset;
    return pictuteMarkerSymbol;
}

/**
 * 获取填充样式
 * 使用之前需要加载如下js
 * <p>
 * 	dojo.require("esri.symbols.SimpleFillSymbol");
 * 	dojo.require("esri.Color");
 * </p>
 * @param options 可选参数，style:样式 backwarddiagonal | cross | diagonalcross | forwarddiagonal } horizontal | none | solid | vertical
 * outline:外边框,color:填充色
 * @returns {*}
 */
function getSimpleFileSymbol(options) {
    options = $.extend({
        style: esri.symbols.SimpleFillSymbol.STYLE_SOLID,
        outline: getSimpleLineSymbol(),
        color: [255, 0, 0]
    }, options || {});
    return new esri.symbols.SimpleFillSymbol(options.style, options.outline, new esri.Color(options.color));
}

/**
 * 获取填充样式
 * 使用之前需要加载如下js
 * <p>
 * 	dojo.require("esri.symbols.PictureFillSymbol");
 * </p>
 * @param options 可选参数，
 * style:样式 backwarddiagonal | cross | diagonalcross | forwarddiagonal } horizontal | none | solid | vertical
 * outline:外边框,color:填充色
 * @returns {*}
 */
function getPictureFileSymbol(options) {
    options = $.extend({
        url:'',
        outline: getSimpleLineSymbol(),
        width:20,
        height:20
    }, options || {});
    return new esri.symbols.PictureFillSymbol(options.url, options.outline, options.width, options.height);
}

/**
 * 获取文本图标
 * 使用之前需要加载如下js
 * <p>
 * 	dojo.require("esri.symbols.TextSymbol");
 * 	dojo.require("esri.symbols.Font");
 * 	dojo.require("esri.Color");
 * </p>
 * @param text 文本内容
 * @param options 可选参数，fontSieze：字体大小，fontStyle:italic | normal | oblique
 * fontWeight: normal | small-caps
 * fontWeight: bold | bolder | lighter | normal
 * fontFamily:字体
 * color: 字体颜色
 */
function getTextSymbol(text, options) {
    options = $.extend({
        fontSize: 12,
        fontStyle: esri.symbols.Font.STYLE_NORMAL,
        fontVariant: esri.symbols.Font.VARIANT_NORMAL,
        fontWeight: esri.symbols.Font.WEIGHT_NORMAL,
        fontFamily: "黑体",
        color: "#000000",
        yoffset:0,
        xoffset:0
    }, options || {});
    var textSymbol = new esri.symbols.TextSymbol(text, new esri.symbols.Font(options.fontSize, options.fontStyle, options.fontVariant, options.fontWeight, options.fontFamily), new esri.Color(options.color));
    textSymbol.yoffset = options.yoffset;
    textSymbol.xoffset = options.xoffset;
    return textSymbol;
}

/**
 * 移除指定要素
 * @param layerId 图层id
 * @param graphics 要素
 */
function removeGraphics(layerId, graphics){
    var layer = getLayer(layerId);
    if(layer && graphics && graphics.length > 0){
        for(var i=0; i < graphics.length; i++){
            var graphic = graphics[i];
            layer.remove(graphic);
        }
    }
}

/**
 * 显示信息,当坐标点为空时，隐藏
 * 使用之前需要加载如下js
 * <p>
 * 	dojo.require("esri.geometry.Point");
 * </p>
 * @param title 标题
 * @param content 内容
 * @param lon 经度
 * @param lat 纬度
 */
function showInfoWindow(title, content, lon, lat) {
    if (lon && lat) {
        map.infoWindow.setTitle(title || "");
        map.infoWindow.setContent(content);
        map.infoWindow.show(new esri.geometry.Point(lon, lat, map.spatialReference));
    } else
        map.infoWindow.hide();
}


//经纬度转墨卡托
// 经度(lon)，纬度(lat)
function lonLatToMercator(lon, lat){
    var point = new Object();
    lon = lon*1;
    lat = lat*1;
    var x = lon *20037508.342789/180;
    var y = Math.log(Math.tan((90+lat)*Math.PI/360))/(Math.PI/180);
    y = y *20037508.34789/180;
    point.x = x;
    point.y = y;
    return point;
}

//墨卡托转经纬度
function mercatorTolonLat(mercatorX, mercatorY){
    var point = new Object();
    var x = mercatorX/20037508.34*180;
    var y = mercatorY/20037508.34*180;
    y= 180/Math.PI*(2*Math.atan(Math.exp(y*Math.PI/180))-Math.PI/2);
    point.x = x;
    point.y = y;
    return point;
}
//查询wfs服务
function queryFeatureByFilter(layer,options,callBackFunction){
	 options = $.extend({
		 method: "BBOX",
		 resultFormat: "GeoJSON",
		 destionLayer: layer,
		 propertyName: "",
		 keyWord: "",
		 extent :"",
		 srsName:"EPSG:4490",
		 maxFeatures:300,
	    }, options || {});
	 post(basePath+"map/getAreaFeatureByCode",
			 options,
		      function(result){
		      	if(callBackFunction)
		      		callBackFunction(result);
		      });
}
/**
 * 经纬度转换成三角函数中度分表形式
 * @param d
 * @returns
 */
function rad(d) {
	return d*Math.PI/180.0;
}
var EARTH_RADIUS = 6378.137;//地球半径（公里）
/**
 * 计算两个经纬度之间的距离，单位公里
 * @param lon1
 * @param lat1
 * @param lon2
 * @param lat2
 * @returns {Number}
 */
function distanceCalculate(lon1, lat1, lon2, lat2) {
	var radLat1 = rad(lat1);
	var radLat2 = rad(lat2);
	var a = radLat1 - radLat2;
	var b = rad(lon1) - rad(lon2);
	var s = 2*Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2)+Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
	s = Math.round(s*EARTH_RADIUS*10000)/10000;
	return s;
}